<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014-2018 东莞市云拓互联网络科技有限公司
// +----------------------------------------------------------------------
// | 官方网站:http://www.ytclouds.net
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------

namespace app\appusers\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;

/**
 * 赛事管理
 * @author Anyon <zoujingli@qq.com>
 */
class Competition extends BasicAdmin
{

    /**
     * 定义当前操作表名
     * @var string
     */
    public $table = 'CompetitionArticle';

    public function index()
    {
        $this->title = '赛事列表';
        $get = $this->request->get();
        $db = Db::name($this->table)->where(['is_deleted' => '0']);

        foreach (['title', 'author'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        if (isset($get['create_at']) && $get['create_at'] !== '') {
            list($start, $end) = explode(' - ', $get['create_at']);
            $db->whereBetween('create_at', ["{$start} 00:00:00", "{$end} 23:59:59"]);
        }
        return parent::_list($db->order('create_at desc'));
    }

    /**
     * 添加赛事
     * @return array|string
     */
    public function add(){
        $this->title = '添加赛事';
        return $this->_form($this->table, 'form');
    }

    /**
     * 编辑赛事
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function edit(){
        $this->title = '编辑赛事';
        return $this->_form($this->table, 'form');
    }

    /**
     * 删除赛事
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("赛事删除成功！", '');
        }
        $this->error("赛事删除失败，请稍候再试！");
    }



}
